package com.jdyfy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdyfy.entity.Announcement;
import com.jdyfy.entity.Publicity;
import com.jdyfy.entity.PublicityClarify;
import com.jdyfy.mapper.PublicityClarifyMapper;
import com.jdyfy.result.Result;
import com.jdyfy.service.PublicityClarifyService;
import com.jdyfy.service.PublicityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/jdyfy")
@Slf4j
public class PublicityController {

    @Autowired
    private PublicityService publicityService;

    @Autowired
    private PublicityClarifyService publicityClarifyService;

    @Autowired
    private PublicityClarifyMapper publicityClarifyMapper;

    //根据项目编号集合查询每个项目对应的公告信息，集合
    @PostMapping("/getPublicitys")
    public List<Publicity> getPublicitys(@RequestBody List<String> projectNumbers) {
        return publicityService.getPublicitysByProjectNumbers(projectNumbers);
    }

    //公告添加或修改
    @PostMapping("/publicity/save")
    public boolean addOrUpdatePublicity(@RequestBody Publicity publicity) {
        return publicityService.saveOrUpdate(publicity);
    }

    //公告删除
    @DeleteMapping("/publicity/delete")
    public boolean deletePublicity(@RequestBody String publicityName) {
        // 去掉首尾的双引号
        if (publicityName.startsWith("\"") && publicityName.endsWith("\"")) {
            publicityName = publicityName.substring(1, publicityName.length() - 1);
        }
        return publicityService.removeById(publicityName);
    }

    //公告澄清添加或修改
    @PostMapping("/publicityClarify/save")
    public boolean addPublicityClarify(@RequestBody PublicityClarify publicityClarify) {
        return publicityClarifyService.saveOrUpdate(publicityClarify);
    }

    //根据项目编号查询澄清公告
    @GetMapping("/publicity/clarifyByProjectNumber")
    public Result<List<PublicityClarify>> getPublicityClarifyByProjectNumber(@RequestParam String projectNumber) {
        return Result.success(publicityClarifyMapper.selectList(new QueryWrapper<PublicityClarify>().like("publicity_name", projectNumber)));
    }
}
